//
//  optionViewBackground.m
//  mtt
//
//  Created by Gerfried Mikusch on 07.09.10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "optionViewBackground.h"


@implementation optionViewBackground


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
		
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
	
	CGContextRef context = UIGraphicsGetCurrentContext();		//Context for drawing
	CGContextSetShadow(context, CGSizeMake(-3, -3), 8);			//Set the shadow for the context
	

	/*Drawing the Path
	 
	 Folgender Pfad soll gezeichnet werden (Koordinaten in View Koordinaten)
	 Größe der OptionView 80x130; Radien für die Kreise: 20px; Mittelpunkte der Kreise: a1c, a2c
	 
			     5				
			    /|				
			   / |				
	a1c.3-----4.a2c				
	 . /         |				
	 ./          |				
	 2           |
	 |           |
	 |           |
	 |           |
	 |           |
	 1-----------6
	 
	 Der Pfad muss in einen eigenen Layer, da er geclippt wird und somit der context die größe des Pfades hat.
	 Dadurch wäre es nicht möglich einen Schatten zu zeichen, da dieser ausserhalb des geclippten Bereichs liegen muss.
	 Der normale context hat jedoch die Größe der View und kann somit den Schatten darstellen
	 
	 */
	
	CGLayerRef pathLayer = CGLayerCreateWithContext(context, rect.size, nil);	//Layer for the path
	CGContextRef pathContext = CGLayerGetContext(pathLayer);					//Context for the path
	
	//Points for the Path
	CGPoint p1 = CGPointMake(9, 130);
	CGPoint p2 = CGPointMake(9, 41);
	CGPoint p3 = CGPointMake(29, 21);
	CGPoint p4 = CGPointMake(60, 21);
	CGPoint p5 = CGPointMake(80, 0);
	CGPoint p6 = CGPointMake(80, 130);
	CGPoint a1c = CGPointMake(9, 21);
	CGPoint a2c = CGPointMake(80, 21);
	
	CGMutablePathRef bgPath = CGPathCreateMutable();
		
	CGPathMoveToPoint(bgPath, nil, p1.x, p1.y);						//Move to Point 1
	CGPathAddLineToPoint(bgPath, nil, p2.x, p2.y);					//Add Line to Point 2
	CGPathAddArcToPoint(bgPath, nil, a1c.x, a1c.y, p3.x, p3.y, 20);	//Add Arc to Point 3
	CGPathAddLineToPoint(bgPath, nil, p4.x, p4.y);					//Add Line to Point 4
	CGPathAddArcToPoint(bgPath, nil, a2c.x, a2c.y, p5.x, p5.y, 20);	//Add Arc to Point 5
	CGPathAddLineToPoint(bgPath, nil, p6.x, p6.y);					//Add Line to Point 6
	CGPathCloseSubpath(bgPath);										//Close Path
	
	CGContextAddPath(pathContext, bgPath);					//Add Path to context of the path Layer
	CGContextClip(pathContext);								//Clip the Path-Context to the Path. Necessary to fill it with a Gradient.
	
	//Gradient
	CGFloat components[10] = { 
		0,		//CYAN-Value of brighter Color
		0,		//MAGENTA-Value of brighter Color
		1,		//YELLOW-Value of brighter Color
		0.28,	//BLACK-Value of brighter Color
		1,		//ALPHA-Value of brighter Color
		0,		//CYAN-Value of darker Color
		0,		//MAGENTA-Value of darker Color
		1,		//YELLOW-Value of darker Color
		0.66,	//BLACK-Value of darker Color
		1		//ALPHA-Value of darker Color
	};
	
	CGFloat locations[] = { 0.2, 1 };	//Locations of Gradient Color 
	
	CGGradientRef gradient = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceCMYK(), components, locations, 2);
	
	//Draw the Gradient to the Clipped Path-Context. Gradient starts at left and goes to right
	CGContextDrawLinearGradient(pathContext, gradient, CGPointMake(80, 65), CGPointMake(0, 65), kCGGradientDrawsAfterEndLocation);
	
	//Draw the Layer with the Path in it
	CGContextDrawLayerInRect(context, rect, pathLayer);
	
}

- (void)dealloc {
    [super dealloc];
}


@end
